package com.kaiyun.sharding;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kaiyun.sharding.mapper.CourseBaseMapper;
import com.kaiyun.sharding.mapper.CourseInfoMapper;
import com.kaiyun.sharding.mapper.DictionaryMapper;
import com.kaiyun.sharding.model.CourseBase;
import com.kaiyun.sharding.model.CourseInfo;
import com.kaiyun.sharding.model.Dictionary;
import com.kaiyun.sharding.model.param.Course;
import com.kaiyun.sharding.service.CourseInfoService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.List;

/**
 * 测试读写分离
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class ShardingApplicationTests5 {

    @Autowired
    private DictionaryMapper dictionaryMapper;
    @Autowired
    private CourseBaseMapper courseBaseMapper;
    @Autowired
    private CourseInfoMapper courseInfoMapper;
    @Autowired
    private CourseInfoService courseInfoService;


    /**
     * 单个插入courseInfo
     */
    @Test
    public void insertOneCourseInfo() {
        for (int i = 0; i <10 ; i++) {
            CourseInfo cc=new CourseInfo();
            cc.setName("姓名aa"+i);
            cc.setStatus(1);
            cc.setUserId((long)i);
            courseInfoMapper.insert(cc);
        }
    }

    /**
     * 批量插入courseInfo
     */
    @Test
    public void batchInsertCourseInfo(){
        List<CourseInfo> list=new ArrayList<>();

        for (int i = 0; i <100 ; i++) {
            CourseInfo cc=new CourseInfo();
            cc.setName("姓名批量"+i);
            cc.setStatus(1);
            cc.setUserId(100L);
            list.add(cc);
        }
        courseInfoService.saveBatch(list);
    }


    /**
     * 插入公共表
     */
    @Test
    public void insertOneDictionary() {
        for (int i = 0; i <10 ; i++) {
            Dictionary cc=new Dictionary();
            cc.setName("name"+i);
            cc.setCode("act");
            dictionaryMapper.insert(cc);
        }
    }

    /**
     * 插入courseBase
     */
    @Test
    public void insertOneCourseBase() {
        for (int i = 0; i <10 ; i++) {
            CourseBase cc=new CourseBase();
            cc.setName("namekk"+i);
            cc.setUserId((long)i);
            courseBaseMapper.insert(cc);
            log.info("插入的id:"+cc.getId());
        }
    }


    @Test
    public void delete() {
        dictionaryMapper.deleteById(1327184014246187010L);
    }

    /**
     * 用mybatis-plus方式获取列表
     */
    @Test
    public void getCourseBaseList(){
        QueryWrapper<CourseBase> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        Page<CourseBase> p=new Page<>(1,5);
        IPage<CourseBase> iPage = courseBaseMapper.selectPage(p, queryWrapper);
        for (CourseBase base:iPage.getRecords()) {
             log.info("结果：{}",base.toString());
        }
        log.info("总数："+ iPage.getTotal());
    }


    /**
     * 单库分表关联查询
     */
    @Test
    public void getCourseInfoList(){
        Page<Course> page=new Page<>(1,2);
        List<Course> list = courseInfoMapper.getList(page, "批量");
        list.forEach(System.out::println);
        log.info("总数："+page.getTotal());

    }

    /**
     * 不带分页关联查询，单库单表关联
     */
    @Test
    public void getCourseInfoList2(){

        List<Course> list = courseInfoMapper.getList2("批量");
        list.forEach(System.out::println);
    }


    /**
     * 插入共表
     */
    @Test
    public void addOne3() {
        for (int i = 0; i <10 ; i++) {
            Dictionary cc=new Dictionary();
            cc.setName("姓名"+i);
            cc.setCode("222dd");
            dictionaryMapper.insert(cc);
        }
    }

    /**
     * 获取单个表
     */
    @Test
    public void getCourseList(){

        Page<Course> page=new Page<>(1,2);
        List<Course> list=courseBaseMapper.getList2(page,1L);

        for (Course base:list) {
            log.info("结果："+base.toString());
        }
        log.info("总数："+page.getTotal());

    }

    /**
     * 分库分表关联查询
     */
    @Test
    public void getCourseBaseList2(){

        Page<Course> page=new Page<>(1,2);
        CourseBase courseBase=new CourseBase();
        courseBase.setName("批量");
        courseBase.setUserId(1L);
        List<Course> list=courseBaseMapper.getList4(null,courseBase);

        for (Course base:list) {
            log.info("结果："+base.toString());
        }
        log.info("总数："+page.getTotal());
    }


    /**
     * 分库和分表字段是不能够更改的,所以下面代码会报错
     */
    @Test
    public void updateCourseBase(){
        courseBaseMapper.updateCourseBase(1L,1327088766660661250L);
    }





}
